<?php
/**
 * Created by PhpStorm.
 * User: jim
 * Date: 2016-12-28
 * Time: 22:39
 */

namespace App\Http\Controllers\home;


use App\Http\Controllers\Controller;
use App\Models\Temp_phone;
use App\Org\SendTemplateSMS;
use Illuminate\Http\Request;

class PcodeController extends Controller
{
    public function sendCode(Request $request)
    {

        $phone = $request->input("phone");
        $code = "";
        $str = "1234567890";
        $_len = strlen($str) - 1;
        for ($i = 0; $i < 4; $i++) {
            $code .= $str[mt_rand(0, $_len)];
        }
        $ob = new SendTemplateSMS;

        $res = $ob->sendTemplateSMS($phone, array($code, 5), 1);
        var_dump($res);
        if ($res->status == 0) {
            $temp = Temp_phone::where('phone', $phone)->where('deadline', '>', time() + 300)->orderBy('deadline', 'desc')->first();
            var_dump($temp);
            if ($temp==null) {
                $temp = new Temp_phone();
                $temp->phone = $phone;
                $temp->code = $code;
                $temp->deadline = time() + 60;
                $temp->save();
            }
        }
    }

    public function checkCode()
    {

    }
}